package com.ruoyi.web.controller.cat;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.cat.domain.CatArticletaskTable;
import com.ruoyi.cat.domain.DTO.ArticletaskDTO;
import com.ruoyi.cat.domain.VO.IncreaseVO;
import com.ruoyi.cat.service.ICatArticletaskTableService;
import com.ruoyi.common.utils.IpRegion;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.ip.IpUtils;
import com.ruoyi.web.controller.api.IpAddressUtil;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 文章任务Controller
 * 
 * @author ruoyi
 * @date 2024-12-24
 */
@RestController
@RequestMapping("/system/articleTask")
public class CatArticletaskTableController extends BaseController
{
    @Autowired
    private ICatArticletaskTableService catArticletaskTableService;

    /**
     * 查询文章任务列表
     */
    @PreAuthorize("@ss.hasPermi('system:table:list')")
    @GetMapping("/list")
    public TableDataInfo list(CatArticletaskTable catArticletaskTable)
    {
        startPage();
        catArticletaskTable.setArticleMode("0");
        List<CatArticletaskTable> list = catArticletaskTableService.selectCatArticletaskTableList(catArticletaskTable);
        return getDataTable(list);
    }

    /**
     * 导出文章任务列表
     */
    @PreAuthorize("@ss.hasPermi('system:table:export')")
    @Log(title = "文章任务", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CatArticletaskTable catArticletaskTable)
    {
        List<CatArticletaskTable> list = catArticletaskTableService.selectCatArticletaskTableList(catArticletaskTable);
        ExcelUtil<CatArticletaskTable> util = new ExcelUtil<CatArticletaskTable>(CatArticletaskTable.class);
        util.exportExcel(response, list, "文章任务数据");
    }

    /**
     * 获取文章任务详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:table:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(catArticletaskTableService.selectCatArticletaskContentTableById(id));
    }


    /**
     * 新增文章任务
     */
    //@PreAuthorize("@ss.hasPermi('system:table:add')")
    //@Log(title = "文章任务", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(HttpServletRequest request, @RequestBody CatArticletaskTable catArticletaskTable)
    {
      /*  String ipAddress = IpUtils.getIpAddr(request);
        IpRegion currentArea = IpAddressUtil.parseIpLocation(ipAddress);
        if (currentArea != null) {
            catArticletaskTable.setCity(currentArea.getCity());
            catArticletaskTable.setProvince(currentArea.getProvince());
        }*/
        return toAjax(catArticletaskTableService.insertCatArticletaskTable(catArticletaskTable));
    }

    @PostMapping("advertTime")
    public AjaxResult advertTime(@RequestBody CatArticletaskTable catArticletaskTable)
    {
        return toAjax(catArticletaskTableService.updateVisitors(catArticletaskTable));
    }

    /**
     * 下架文章任务
     */
    @PostMapping("offShelf")
    public AjaxResult offShelf(@RequestBody CatArticletaskTable catArticletaskTable)
    {
        if (StringUtils.isNotEmpty(catArticletaskTable.getArticleState())){
            //下载
           // catArticletaskTable.setArticleExamine("3");
            catArticletaskTable.setActivityState("2");
        }
        return toAjax(catArticletaskTableService.updateCatArticletaskTable(catArticletaskTable));
    }

    /**
     * 修改文章任务
     */
    @PreAuthorize("@ss.hasPermi('system:table:edit')")
    @Log(title = "文章任务", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CatArticletaskTable catArticletaskTable)
    {
        return toAjax(catArticletaskTableService.updateCatArticletaskTable(catArticletaskTable));
    }

    /**
     * 删除文章任务
     */
    @PreAuthorize("@ss.hasPermi('system:table:remove')")
    @Log(title = "文章任务", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(catArticletaskTableService.deleteCatArticletaskTableByIds(ids));
    }

    //增加目标值接口
    @PostMapping("increaseValue")
    public AjaxResult increaseValue(@RequestBody IncreaseVO increaseVO)
    {
        return catArticletaskTableService.increaseValue(increaseVO);
    }
}
